﻿Imports FinanceiroBLL
Imports FinanceiroDAL.ModuloConexao
Public Class ContasUC
    Dim objBLL As New ContasBLL
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs)
        If (PLocalizar.Visible = True) Then
            PLocalizar.Visible = False
        Else
            PLocalizar.Visible = True
            TSBLocalizar.Checked = True
        End If
    End Sub
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        Dispose()
    End Sub
#Region "Cadastrar"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Dim contas As New CadastroContasFRM
        TSBNovo.BackColor = Color.Gainsboro
        CadastroContasFRM.ShowDialog()
        carregagrid()
        TSBNovo.BackColor = Color.Transparent
    End Sub
#Region "Carrega Grid"
    Public Sub carregagrid()
        Dim dt As New DataTable
        dt = objBLL.localizar()
        DGVDados.DataSource = dt
        DGVDados.Refresh()
    End Sub
#End Region
#End Region

    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("con_codigo") Is Nothing) Then
                    objBLL = New ContasBLL(Val(DGVDados.Rows(linha.Index).Cells("con_codigo").Value.ToString()))
                End If
            Next
            Dim conta As New CadastroContasFRM
            If (objBLL.codigo >= 0) Then
                conta.TCodigo.Text = objBLL.codigo
                conta.TConta.Text = objBLL.descricao
                conta.CBTpConta.SelectedValue = objBLL.tipoConta
                conta.CBBanco.SelectedValue = objBLL.banco
                conta.DTPAbertura.Text = objBLL.dtabertura
                If conta.DTPFechamento.CustomFormat = "" Then
                    conta.DTPFechamento.CustomFormat = " "
                Else
                    conta.DTPFechamento.Text = objBLL.dtfechamento
                End If
                conta.TAgencia.Text = objBLL.agencia
                conta.TContaCorrente.Text = objBLL.contacorrente
                conta.TGerente.Text = objBLL.gerente
                conta.TEmail.Text = objBLL.email
                conta.TSaldoInicial.Text = objBLL.saldoinicial
                conta.CBStatus.SelectedValue = objBLL.status
                TSBAlterar.BackColor = Color.Gainsboro
                conta.LContas.Text = objBLL.descricao
                conta.ShowDialog()
                carregagrid()
            End If

        Catch ex As Exception

        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try

    End Sub
#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Dim ret As Boolean
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("con_codigo") Is Nothing) Then
                objBLL = New ContasBLL(Val(DGVDados.Rows(linha.Index).Cells("con_codigo").Value.ToString()))
            End If
        Next
        If (objBLL.codigo > 0) Then
            TSBExcluir.BackColor = Color.Gainsboro
            If (MessageBox.Show("Confirma a exclusão da categoria: " & objBLL.descricao & "?", "Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                ret = objBLL.Excluir(objBLL.codigo)
                If (ret <> False) Then
                    MsgBox("Exclusão efetuada com sucesso!", MsgBoxStyle.Information)
                    carregagrid()
                Else
                    MsgBox("Conta não excluída", MsgBoxStyle.Exclamation)
                End If
            Else
                MsgBox("Conta não excluída", MsgBoxStyle.Exclamation)
            End If
        End If
        TSBExcluir.BackColor = Color.Transparent
    End Sub
#End Region

    Private Sub TSBLocalizar_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If (PLocalizar.Visible) Then
            PLocalizar.Visible = False
            CBFiltro.Visible = False
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PLocalizar.Visible = True
            CBFiltro.Visible = True
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
    End Sub

    Private Sub ContasUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CBFiltro.SelectedIndex = 1
        carregagrid()
    End Sub

    Private Sub CBFiltro_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
        End If
        TXTLocalizar.Text = ""
        carregagrid()

    End Sub

    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

    Private Sub TSBMovimentacao_Click(sender As System.Object, e As System.EventArgs) Handles TSBMovimentacao.Click
        Dim frmcm As New ContasMovimentoFRM
        frmcm.ShowDialog()
        frmcm.Dispose()
        frmcm = Nothing
    End Sub

    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        TSBAlterar_Click(Nothing, Nothing)
    End Sub
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosfinFRM
            Dim rpt As New ContaFichaREL

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer2.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer2.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub

    Private Sub TSBRelatorio_Click(sender As System.Object, e As System.EventArgs) Handles TSBRelatorio.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("con_codigo") Is Nothing) Then
                objBLL = New ContasBLL(Val(DGVDados.Rows(linha.Index).Cells("con_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.codigo > 0) Then gerarelatrio(objBLL.descricao, "{vw_FinContas.con_codigo}='" & objBLL.codigo & "'")
        End If
    End Sub

    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Try
            Select Case CBFiltro.Text
                Case "Código"
                    DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
                Case "Descrição"
                    DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
                Case "Banco"
                    DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
                Case "Tipo"
                    DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
                    'Case "Status"
                    '    DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            End Select
            If TXTLocalizar.Text = "" Then
                DGVDados.DataSource = objBLL.buscar("", 7)
            End If
            DGVDados.Refresh()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = objBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
End Class
